define(function(require, exports, module) {
	//'use strict';
    
    var rk = require('rk');
    
    
    $.widget('rk.colleagueDepartMemberTabCtrl', {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='colleague_depart_member_follow']", function(){
                var follow = $(this);
                var uid = follow.attr("uid");
                var isFollow = follow.attr("follow");
                if(follow.hasClass('disable')) return;
                follow.addClass('disable');
                if(isFollow == "1") {
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function () {
                        rk.post("/json/oa_relation/delFollow.action", {fId: uid}, function (json) {
                            if (json.status == 0) {
                                follow.removeClass("has_follow");
                                follow.addClass("no_follow");
                                follow.attr("follow", "0");
                                follow.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            follow.removeClass('disable');
                        });
                    },function(){});
                }else if(isFollow == "0") {
                    rk.post("/json/oa_relation/addFollow.action", {fId: uid}, function (json) {
                        if (json.status == 0) {
                            follow.removeClass("no_follow");
                            follow.addClass("has_follow");
                            follow.attr("follow","1");
                            follow.attr("title",rk.i18n('COLLEAGUE_CANCEL_FOLLOW'));
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        } else {
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        follow.removeClass('disable');
                    });
                }
            });
            elem.on("click", "[act='colleague_depart_member_setAdmin']", function(){
                var setAdmin = $(this);
                var isAdmin = setAdmin.attr("isAdmin");
                var uid = setAdmin.attr("uid");
                var text = rk.i18n('COLLEAGUE_CONFIRM_ADD_ADMIN');
                if(isAdmin == "1"){
                    text = rk.i18n('COLLEAGUE_CONFIRM_CANCEL_ADMIN');
                }
                rk.confirm(text, function () {
                    if(isAdmin == "1") {
                        rk.post("/json/oa_group/admin-cancel.action", {uid: uid,gid:opt.gid}, function (json) {
                            if (json.status == 0) {
                                setAdmin.attr("isAdmin","0");
                                setAdmin.html(rk.i18n('COLLEAGUE_ADD_ADMIN'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else if(json.status === 217001){
                                rk.noticeError(rk.i18n('COLLEAGUE_NO_ADMIN_AUTHORITY'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                        });
                    }else if(isAdmin == "0"){
                        rk.post("/json/oa_group/admin-grant.action", {uid: uid,gid:opt.gid}, function (json) {
                            if (json.status == 0) {
                                setAdmin.attr("isAdmin","1");
                                setAdmin.html(rk.i18n('COLLEAGUE_CANCEL_ADMIN'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else if(json.status === 217001){
                                rk.noticeError(rk.i18n('COLLEAGUE_NO_ADMIN_AUTHORITY'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                        });
                    }
                }, function () {
                    //取消
                });
            });
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            me.loaddata({pageNo:1,pageSize:20},elem);
        },
        loaddata:function(param,divs){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            if(!param){
                param = {pageNo:1,pageSize:20};
            }
            param.gid = opt.gid;
            rk.post("/json/oa_group/member.action",param,function(json){
                if(json.status === 0){
                    if(json.canAccess) {
                        if(json.members.length > 0) {
                            require.async(["oa/tmpl/colleague/content/tpl_colleague_depart_content_member.tpl"], function (temp) {
                                var html = rk.templateText(temp, json);
                                divs.html(html);
                                //aolleague_depart_member_page
                                elem.find('[act="colleague_depart_member_page"]').rkPaging({
                                    pagenum: json.pageNo,  //（可选）当前页数，默认1
                                    pagesize: json.pageSize,  //（可选）当前每页显示条数，默认20
                                    rowscount: json.dataCount, //所有数据数
                                    onChange: function (pagenum) {
                                        var params = param;
                                        params.pageNo = pagenum;
                                        params.pageSize = json.pageSize;
                                        me.loaddata(params, divs);
                                    }
                                });
                                elem.find("[act='colleague_depart_member_follow']").beTip({dir:"left"});
                            });
                        }else{
                            require.async(["oa/tmpl/colleague/tpl_colleague_none.tpl"], function (temp) {
                                var html = rk.templateText(temp, {text: rk.i18n('COLLEAGUE_NO_PEOPLE')});
                                divs.html(html);
                            });
                        }
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_NO_AUTHORITY_ERROR'));
                    }
                }else{
                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                }
            });
        }
    });
});